package server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @author 14548
 */
public class Server {
    public static void main(String[] args) {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup wokerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, wokerGroup)
                    .channel(NioServerSocketChannel.class)
                    //true为开启tcp底层心跳协议
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    //true为关闭Nagle算法，可以提高数据传输的时效性
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childHandler(new ServerInitializer());

            System.out.println("服务器启动成功");
            ChannelFuture channelFuture = serverBootstrap.bind(8080).sync();
            System.out.println("服务器绑定端口成功，端口:8080");
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            bossGroup.shutdownGracefully();
            wokerGroup.shutdownGracefully();
        }
    }
}
